#include "Engine.h"
#include "cs315.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>


Location::Location(int newRow, int newCol)
{
	row = newRow;
	col = newCol;
}

Location::~Location()
{

}

Location::Location()
{
	row =0;
	col =0;
}

void Location::setRow(int newRow)
{
	row = newRow;
}
	
int	 Location::getRow()
{
	return row;
}

void Location::setCol(int newCol)
{
	col = newCol;
}
	
int	 Location::getCol()
{
	return col;
}


void Character::printCharacter()
{
	printf("This character is # %d, and is at %d,%d\n race: %d, class: %d\n ATK: %d\n DEF: %d\n HP: %d\n SPD: %d\n Weapon level: %d\n Armor level: %d\n",charID, locRow, locCol, race, charClass, ATK, DEF, HP, SPD, weapon, armor);
}


void Character::setCharID(int newCharID)
{
	charID = newCharID;
}
	
int	 Character::getCharID()
{
	return charID;
}

void Character::setRace(int newRace)
{
	race = newRace;
}

int Character::getRace()
{
	return race;
}

void Character::setCharClass(int newCharClass)
{
	charClass = newCharClass;
}
	
int Character::getCharClass()
{
	return charClass;
}

void Character::setATK(int newATK)
{
	ATK = newATK;
}
	
int Character::getATK()
{
	return ATK;
}

void Character::setDEF(int newDEF)
{
	DEF = newDEF;
}
	
int Character::getDEF()
{
	return DEF;
}

void Character::setHP(int newHP)
{
	HP = newHP;
}
	
int Character::getHP()
{
	return HP;
}

void Character::setSPD(int newSPD)
{
	SPD = newSPD;
}
	
int Character::getSPD()
{
	return SPD;
}

void Character::setWeapon(int newWeapon)
{
	weapon = newWeapon;
}
	
int Character::getWeapon()
{
	return weapon;
}

void Character::setArmor(int newArmor)
{
	armor = newArmor;
}
	
int Character::getArmor()
{
	return armor;
}


Character::Character(int NEWID)
{
	charID = NEWID;
	race = CS315::RandInt(5);
	charClass = CS315::RandInt(5);	 
	ATK = CS315::RandInt(10);
	DEF = CS315::RandInt(10);
	HP = CS315::RandInt(10);
	SPD = CS315::RandInt(10);
	weapon = CS315::RandInt(4);
	armor = CS315::RandInt(4);
	locRow = -1;
	locCol = -1;
}

Character::Character(int NEWID,int NEWrace,int NEWcharClass,int NEWatk, int NEWdef, int NEWhp, int NEWspd, int NEWweapon,int NEWarmor, int newLocRow, int newLocCol)
{
	charID = NEWID;
	race = NEWrace;
	charClass = NEWcharClass;	 
	ATK = NEWatk;
	DEF = NEWdef;
	HP = NEWhp;
	SPD = NEWspd;
	weapon = NEWweapon;
	armor = NEWarmor;
	locRow = newLocRow;
	locCol = newLocCol;
}



Character::Character()
{
	charID = 0;
	race = 1;
	charClass = 1;	 
	ATK = 1;
	DEF = 1;
	HP = 1;
	SPD = 1;
	weapon = 1;
	armor = 1;
}


Character::~Character()
{

}


void TestBoard::addChar(Character* newChar, int newRow, int newCol)
{
	
	board[newRow][newCol] = *newChar;
	board[newRow][newCol].locRow = newRow;
	board[newRow][newCol].locRow = newCol;
	newChar->locRow = newRow;
	newChar->locCol = newCol;
}

void TestBoard::moveChar(Character *newChar, int oldRow, int oldCol, int newRow, int newCol)
{
	
	printf("character to move is \n"  );
	newChar->printCharacter();

	board[oldRow][oldCol].charID = -1;
	board[oldRow][oldCol].locRow = -1;
	board[oldRow][oldCol].locCol = -1;
	printf("old character came from %d,%d and is now \n", oldRow,oldCol  );
	board[oldRow][oldCol].printCharacter();

	board[newRow][newCol] = *newChar;
	board[newRow][newCol].locRow = newRow;
	board[newRow][newCol].locCol = newCol;
}
	 
Character TestBoard::getChar(int fromRow, int fromCol)
{
	return board[fromRow][fromCol];
}

void TestBoard::initializeBoard()
{
	for(int i=0;i<10;i++)
		{	
		for( int j=0;j<10;j++)
			{
				Character* c = new Character(i+j);
				board[i][j] = *c;
				board[i][j].charID = -1;
			}
		}
}

void TestBoard::getSymbols()
{
	
	for(int i=0;i<10;i++)
		{	
		for( int j=0;j<10;j++)
			{
				if(board[i][j].charID >= 0)
				{
					symbols[i][j] = board[i][j].charID + 48;
				}
				else
				{
					symbols[i][j] = '.';
				}
			}
		}
}

int TestBoard::isValid(int row, int col)  // Returns -1 if location is not on board, Returns 1 if location is filled, Returns 0 if VALID
{
	if(row < 0 || col <0 || row > 9 || col > 9)
	{
		printf("Location is invalid\n");
		return -1;
	}
	else if(board[row][col].getCharID() != -1)
	{
		printf("Location is filled\n");
		return 1;
	}
	else
	{
		printf("Location is VALID\n");
		return 0;
	}
}

void TestBoard::printBoard()
{
	getSymbols();
	printf("  0 1 2 3 4 5 6 7 8 9\n");
	printf("0 %c %c %c %c %c %c %c %c %c %c\n",symbols[0][0], symbols[0][1], symbols[0][2], symbols[0][3], symbols[0][4],symbols[0][5], symbols[0][6], symbols[0][7], symbols[0][8], symbols[0][9]);
	printf("1 %c %c %c %c %c %c %c %c %c %c\n",symbols[1][0], symbols[1][1], symbols[1][2], symbols[1][3], symbols[1][4],symbols[1][5], symbols[1][6], symbols[1][7], symbols[1][8], symbols[1][9]);
	printf("2 %c %c %c %c %c %c %c %c %c %c\n",symbols[2][0], symbols[2][1], symbols[2][2], symbols[2][3], symbols[2][4],symbols[2][5], symbols[2][6], symbols[2][7], symbols[2][8], symbols[2][9]);
	printf("3 %c %c %c %c %c %c %c %c %c %c\n",symbols[3][0], symbols[3][1], symbols[3][2], symbols[3][3], symbols[3][4],symbols[3][5], symbols[3][6], symbols[3][7], symbols[3][8], symbols[3][9]);
	printf("4 %c %c %c %c %c %c %c %c %c %c\n",symbols[4][0], symbols[4][1], symbols[4][2], symbols[4][3], symbols[4][4],symbols[4][5], symbols[4][6], symbols[4][7], symbols[4][8], symbols[4][9]);
	printf("5 %c %c %c %c %c %c %c %c %c %c\n",symbols[5][0], symbols[5][1], symbols[5][2], symbols[5][3], symbols[5][4],symbols[5][5], symbols[5][6], symbols[5][7], symbols[5][8], symbols[5][9]);
	printf("6 %c %c %c %c %c %c %c %c %c %c\n",symbols[6][0], symbols[6][1], symbols[6][2], symbols[6][3], symbols[6][4],symbols[6][5], symbols[6][6], symbols[6][7], symbols[6][8], symbols[6][9]);
	printf("7 %c %c %c %c %c %c %c %c %c %c\n",symbols[7][0], symbols[7][1], symbols[7][2], symbols[7][3], symbols[7][4],symbols[7][5], symbols[7][6], symbols[7][7], symbols[7][8], symbols[7][9]);
	printf("8 %c %c %c %c %c %c %c %c %c %c\n",symbols[8][0], symbols[8][1], symbols[8][2], symbols[8][3], symbols[8][4],symbols[8][5], symbols[8][6], symbols[8][7], symbols[8][8], symbols[8][9]);
	printf("9 %c %c %c %c %c %c %c %c %c %c\n",symbols[9][0], symbols[9][1], symbols[9][2], symbols[9][3], symbols[9][4],symbols[9][5], symbols[9][6], symbols[9][7], symbols[9][8], symbols[9][9]);

}

TestBoard::TestBoard()
{

}


TestBoard::~TestBoard()
{

}


Engine::Engine()
{
	board = new TestBoard();
}
	
Engine::~Engine()
{

}
	
void Engine::initializeGame()
{

}

void Engine::move()
{
	board->moves[0] = -1;   //Clear move list
	board->moves[1] = -1;
	board->moves[2] = -1;
	board->moves[3] = -1;
	board->moves[4] = -1;
	board->moves[5] = -1;
	board->moves[6] = -1;
	board->moves[7] = -1;
	board->moves[8] = -1;
	board->moves[9] = -1;

	Character* curChar = characters[curCharID];
	int curRow = curChar->locRow;
	int curCol = curChar->locCol;

	int input = -1;
	int moves = curChar->getSPD();
	int curMove = 0;
	int testResult = -2;

	std::cout << "You may move "<< moves << " spaces." << std::endl;
	std::cout << "Where would you like to move? (Use arrow keys)"<< std::endl;
	while(moves != 0) //Gets move list, while character can still move
	{
		input = getch();
		if(input == 224)
		{
			input = getch();
			switch( input)
			{
				case 72:   //Up arrow
					board->moves[curMove] = 1;
					path[curMove].setRow( curRow - 1);
					path[curMove].setCol( curCol);

					testResult = board->isValid(curRow - 1,curCol);

					if(testResult == 0)
					{

						std::cout << "stored UP as move: " << curMove << std::endl;

						printf("Location was: %d,%d \n",curRow,curCol);

						curRow = curRow-1;
						curCol = curCol;
						printf("Now Location is: %d,%d \n",curRow,curCol);

					}
					break;
				case 80:  //Down arrow
					board->moves[curMove] = 2; 
					path[curMove].setRow( curRow + 1);
					path[curMove].setCol( curCol);

					if(testResult == 0)
					{

						std::cout << "stored DOWN as move: " << curMove << std::endl;
						printf("Location was: %d,%d\n",curRow,curCol);
						curRow = curRow+1;
						curCol = curCol;
						printf("Now Location is: %d,%d\n",curRow,curCol);
					}


					break;
				case 75:  //Left arrow
					board->moves[curMove] = 3; 
					path[curMove].setRow( curRow);
					path[curMove].setCol( curCol - 1);

					
					if(testResult == 0)
					{
						std::cout << "stored LEFT as move: " << curMove << std::endl;
						printf("Location was: %d,%d\n",curRow,curCol);
						curRow = curRow;
						curCol = curCol-1;
						printf("Now Location is: %d,%d\n",curRow,curCol);
					}

					break;
				case 77:  //Right arrow
					board->moves[curMove] = 4; 
					path[curMove].setRow( curRow);
					path[curMove].setCol( curCol + 1);

					if(testResult == 0)
					{
						std::cout << "stored RIGHT as move: " << curMove << std::endl;
						printf("Location was: %d,%d\n",curRow,curCol);
						curRow = curRow;
						curCol = curCol+1;
						printf("Now Location is: %d,%d\n",curRow,curCol);
					}

					break; 
			}
			if(testResult ==0)
			{
				curMove ++;
				moves --;
				testResult = -2;
				std::cout << "You pressed: " << input << std::endl;
				std::cout << "You may move "<< moves << " more spaces." << std::endl;
			}
			else
			{
				testResult = -2;
				printf("Location was: %d,%d\n",curRow,curCol);
				printf("You can't move there! Please select again\n" );
			}
		}
	}

	for each( int i in board->moves ) //Calculate positions on the board for each move in the list, relative to the Characters current position, and ensure each location is unoccupied.
	{

	}


}
	
void Engine::play()
{
	curCharID = 0;
	std::cout << "Begin Game "<< std::endl;
	while(keepPlaying)
	{
		board->printBoard();
		int choice;
		std::cout << "Current Character is # "<< curCharID << std::endl;
		std::cout << "What would you like to do?: "<< std::endl;
		std::cout << "(1) Pass"<< std::endl;
		std::cout << "(2) Move"<< std::endl;
		std::cout << "(3) Attack"<< std::endl;
		std::cout << "(4) Stop Playing"<< std::endl;
		std::cin >> choice;
		std::cout << "You chose: " << choice << std::endl;
		switch( choice)
		{
			case 1:
				curCharID++;
				break;
			case 2:
				move();
				break;
			case 3:
				break;
			case 4:
				keepPlaying = false;
				break;
			
		}
	}
}

int main()
{
	Engine* newEngine = new Engine();

	Character* newCharacter = new Character(0);
	Character* newCharacter1 = new Character(1);

	newEngine->characters[0] = newCharacter;
	newEngine->characters[1] = newCharacter1;
	


	newEngine->board->initializeBoard();
	newEngine->board->printBoard();
	newEngine->board->addChar(newEngine->characters[0], 0,0);
	newCharacter->printCharacter();
	newEngine->board->addChar(newEngine->characters[1], 9,9);
	newCharacter1->printCharacter();
	newEngine->board->printBoard();
	newEngine->board->moveChar(newEngine->characters[0], newEngine->characters[0]->locCol, newEngine->characters[0]->locRow, 5, 5);
	newEngine->characters[0]->printCharacter();
	newEngine->board->board[0][0].printCharacter();
	newEngine->board->board[5][5].printCharacter();
	newEngine->board->printBoard();

	newEngine->play();


	std::cin.get();
	return 0;
}